using System;
using System.Collections.Generic;
using System.Text;
using emp.IDAL.SysManage;
using System.Data.SqlClient;
using System.Data;
using emp.Model.SysManage;
using emp.Utility;

namespace emp.SqlServerDAL.SysManage
{
    public class Privilege : IPrivilege
    {
        #region IPrivilege Members

        private const string SQL_SELECT_BY_ROLE = " SELECT privilege_value FROM system_privilege where role_uid = @Role_ID and menu_uid = @Menu_ID ";

        private const string PARM_USER_UID = "@User_ID";
        private const string PARM_ROLE_UID = "@Role_ID";
        private const string PARM_MENU_UID = "@Menu_ID";

        public int Insert(PrivilegeInfo newInfo)
        {
            //5_3,5_2,6_3,6_2  menu_id -- keyValue
            

            int iRoleID = newInfo.RoleID;
            string sMenuKeyStr = newInfo.MenuKeyStr;

            string sSqlInsert = " delete from system_privilege where role_uid=" + iRoleID.ToString()+";";

            string sMenuIDStr = ",";
            string[] menuArray = sMenuKeyStr.Split(',');
            foreach (string sMenuKey in menuArray)
            {
                string[] keyArray = sMenuKey.Split('_');
                //int keyValue = int.Parse(keyArray[1]);
                if (sMenuIDStr.IndexOf(","+keyArray[0]+",") > -1)
                {
                    sSqlInsert += " update system_privilege set privilege_value = privilege_value*" + keyArray[1] + "  where role_uid = " + iRoleID.ToString() + " and menu_uid= " + keyArray[0] + "; ";
                }
                else
                {
                    sMenuIDStr += keyArray[0] + ",";
                    sSqlInsert += " insert into system_privilege values ( " + iRoleID.ToString() + "," + keyArray[0] + "," + keyArray[1] + " );";
                }

            }

            int returnValue = 0;
            using (SqlConnection conn = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC))
            {
                conn.Open();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    try
                    {
                        returnValue = SQLHelper.ExecuteNonQuery(trans, CommandType.Text, sSqlInsert, null);
                        trans.Commit();
                    }
                    catch (SqlException err)
                    {
                        trans.Rollback();
                        string sMethod = "emp.SqlServerDAL.Privilege.Insert(PrivilegeInfo newInfo)";
                        EventsLog.WriteLog(err, sMethod);
                    }
                }
            }
            return returnValue;

        }

        public int getPrivilegeValue(int iRoleID, int iMenuID)
        {
            int returnValue = 1;

            SqlParameter[] parm = new SqlParameter[]{
                new SqlParameter(PARM_ROLE_UID, SqlDbType.Int, 4,"role_uid"),
                new SqlParameter(PARM_MENU_UID, SqlDbType.Int, 4,"menu_uid")
            };
            parm[0].Value = iRoleID;
            parm[1].Value = iMenuID;

            //Execute a query to read the products
            using (SqlDataReader rdr = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, SQL_SELECT_BY_ROLE, parm))
            {
                while (rdr.Read())
                {
                    returnValue = rdr.GetInt32(0);
                }
            }

            return returnValue;            
        }

        #endregion
    }
}
